{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f0d192f8",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    "1. 创建FaceSet\n",
    "2. 查询FaceSet\n",
    "3. 存入face_token数据\n",
    "4. 获取FaceSet信息数据\n",
    "5. 人脸搜索\n",
    "6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "022e4a77",
   "metadata": {},
   "source": [
    "# 创建人脸库 FaceSet ---->  API"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57bf10fe",
   "metadata": {},
   "source": [
    "* 描述：\n",
    "> 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "* 调用方法\n",
    "> 1. POST\n",
    "\n",
    "* 权限\n",
    "> 所有 API Key 都可以调用本 API。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9dade519",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07918061",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b587e08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "82921071",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create' \n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d874ff4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "195a7342",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '91c64730e23b208dd0626aa7d2226be8',\n",
       " 'time_used': 170,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648469174,a35b498e-9326-4879-b28e-13e74c3c9a83',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "3434c459",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"faceset_token\": \"91c64730e23b208dd0626aa7d2226be8\", \"time_used\": 170, \"face_count\": 0, \"face_added\": 0, \"request_id\": \"1648469174,a35b498e-9326-4879-b28e-13e74c3c9a83\", \"outer_id\": \"\", \"failure_detail\": []}'"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "262c5e84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'91c64730e23b208dd0626aa7d2226be8'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faceset_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "700823fc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "368b3f63",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "1218161c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&amp;=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&amp;=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d1a22c6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets' \n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "4edeb164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "b10167ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 85,\n",
       " 'facesets': [{'faceset_token': '285391efa3d58c229156e44153407c84',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '91c64730e23b208dd0626aa7d2226be8',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648469189,ed300d7c-62ed-4fe5-812c-b21e2361bcd9'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b46555e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'91c64730e23b208dd0626aa7d2226be8'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['facesets'][-1]['faceset_token']  # 取值\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b991fc8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3230504e",
   "metadata": {},
   "source": [
    "# 更新人脸库信息 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "d9011d8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'广州南方学院0栋学生宿舍楼',\n",
    "    'new_outer_id':'NFUSD00',\n",
    "    'user_data':'0层，满员，女生宿舍，460位同学，均已录入face_token'\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_update_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "68f7cd9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "dc33be86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '91c64730e23b208dd0626aa7d2226be8',\n",
       " 'tags': '',\n",
       " 'time_used': 73,\n",
       " 'user_data': '0层，满员，女生宿舍，460位同学，均已录入face_token',\n",
       " 'display_name': '广州南方学院0栋学生宿舍楼',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648470555,244abe31-226c-460e-86aa-62884b685480',\n",
       " 'outer_id': 'NFUSD00'}"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef733b54",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a6e1e182",
   "metadata": {},
   "source": [
    "# 检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "545e1a67",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getdetail_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "    'outer_id':'NFUSD00'\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_getdetail_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "ec415923",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "edf0cd83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '91c64730e23b208dd0626aa7d2226be8',\n",
       " 'tags': '',\n",
       " 'time_used': 98,\n",
       " 'user_data': '0层，满员，女生宿舍，460位同学，均已录入face_token',\n",
       " 'display_name': '广州南方学院0栋学生宿舍楼',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648470931,febddcdc-c65b-4f57-87cc-4542d4d1732f',\n",
       " 'outer_id': 'NFUSD00'}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "424e78d0",
   "metadata": {},
   "source": [
    "# 人脸检测，获取人脸token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "93c2740e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "362626f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    d:\\洋洋\\智超api\\jupyter_projects\\week-04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "9938edc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648471774,0c8f8e1b-dde7-4910-bc06-5f0739334117',\n",
       " 'time_used': 49,\n",
       " 'faces': [{'face_token': '2ef07dafd4e2b6520213395749886353',\n",
       "   'face_rectangle': {'top': 174, 'left': 149, 'width': 195, 'height': 195}}],\n",
       " 'image_id': 'TACOe23emB/P1Le94HhGiQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_key,API_sercret, 'xiaoche.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb318bc6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7007d7db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4b3e234e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    d:\\洋洋\\智超api\\jupyter_projects\\week-04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dfc0ef0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 128,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1649070051,075bb778-8869-40d5-8fba-e86fd19c6c2e'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_sercret, 'xiaoche.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de7ef3c4",
   "metadata": {},
   "source": [
    "# 作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77b7ca23",
   "metadata": {},
   "source": [
    "1. 尝试创建detect.py 并实现功能调用\n",
    "2. 尝试创建analyze.py\\campare.py\\search.py，并实现功能调用\n",
    "3. 尝试存放宿舍4位同学的人脸信息到 人脸数据集（XXX栋XXX宿舍）:\n",
    "> 1.完善faceset信息<br>\n",
    "> 2.实现人脸搜索和人脸对比，打印结果：是本人 OR 不是本人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f62f4c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3bec8148",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create' \n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95ca4064",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e41bc4af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets' \n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_getfacesets_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4942153e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3c287d04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5ccf4969abe5e17dc147e375401282a0'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['facesets'][-1]['faceset_token']  # 取值\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7820817b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9680dd30",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.url\n",
    "\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "# 2.api账户/通行证\n",
    "API_key ='sEuPgWGWaBoEVVYn9zfRDvVA9NTj9JT2'\n",
    "API_sercret ='NUufQurHGXmAS-ujFF0tIfdLDHoK51Nu'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "\n",
    "# 4.准备payload(酬载)\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,  \n",
    "    'faceset_token':faceset_token,\n",
    "    'display_name':'广州南方学院0栋学生宿舍楼',\n",
    "    'new_outer_id':'NFUSD00',\n",
    "    'user_data':'0层，满员，女生宿舍，460位同学，均已录入face_token'\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_update_url, params = payload)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7eb49402",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "16a0ed1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "08e8f630",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    d:\\洋洋\\智超api\\jupyter_projects\\week-04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2ceadf0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 52,\n",
       " 'error_message': 'VOID_REQUEST',\n",
       " 'request_id': '1649328683,e4ed5a27-f41d-46c0-b8a9-4dcad8237d23'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_key,API_sercret, 'xiaoche.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b1e54ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6caaf7e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
